package Logic;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AuditLogDB;
import Model.Approval;
import Model.StaffAppointment;

import com.mysql.jdbc.DatabaseMetaData;

/**
 * Servlet implementation class StaffAppNewSpecific_BLL
 */
@WebServlet("/StaffAppNewSpecific")
public class StaffAppNewSpecific_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StaffAppNewSpecific_BLL() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// TODO Auto-generated method stub

		// javax.servlet.http.HttpSession session = request.getSession();
		// String username = session.getAttribute("username").toString();

		
		int specialtablerownumber = Integer.parseInt(request.getParameter("specialtablerownumber").trim());
		String effectivedate = request.getParameter("effectivedate");
		String enddate = request.getParameter("enddate");
		String appointmentid = request.getParameter("appointmentnormalnew");
		boolean exceedLimit = false;
		String description = request.getParameter("description");
		int groupNumber = -1;
		if (description != null && description != "") {
			// New Group to be created
			System.out.println("Creating new sub group");
			groupNumber = Database.StaffAppointmentGroupDB.getLastNumber() + 1;
			Database.StaffAppointmentGroupDB.createGroup(
					Integer.toString(groupNumber), description);
		} else {
			groupNumber = 1;
		}

		
		for(int i=1; i<=specialtablerownumber; i++)
		{
			String staffid = request.getParameter("staff" + i);
			if(staffid!=null)
			{
		
			String year = request.getParameter("year" + i);
			String yearNo = request.getParameter("yearNo" + i);
			System.out.println("The year no is : " + yearNo);
			String dipcode = request.getParameter("dipcode" + i);
			String saclass = request.getParameter("saclass" + i);
			StaffAppointment sa = new StaffAppointment();
			sa.setAppointmentID(appointmentid);
			sa.setEffectiveDate(effectivedate);
			sa.setApptGrpID(Integer.toString(groupNumber));
			sa.setLoginID(staffid);
			sa.setEndDate(enddate);
			String md5 = Logic.MD5.convert(Long.toString(System
					.currentTimeMillis()) + staffid);

			if (Database.StaffAppointmentDB
					.numberOfAppointments(staffid) >= Database.MaxConcApptDB
					.select()) {
				exceedLimit = true;
			}
			sa.setCreatedBy(
							(String) request.getSession().getAttribute("username"));
			sa.setMd5(md5);
			sa.setYear(year);
			sa.setYearNo(yearNo);
			sa.setDiploma(dipcode);
			sa.setSaclass(saclass);
			
				Database.SpecificStaffAppointmentDB.createAppointmentSpecific(sa);
				System.out.println("insert already");
			

			}
			else{
				
			}
		}
		

		AuditLogDB.insertLog("assign staffs to appointment",
				(String) request.getSession().getAttribute("name"));
		response.sendRedirect("newstaffapp_newappt.jsp?error=-1");
		
		
		
		
		
		
		
		
		
		
		
		/*
		if (request.getParameter("effectivedate") != ""
				&& request.getParameter("selectstaff") != "") {
			int groupNumber = -1;

			String staffs = request.getParameter("selectstaffnew");

			String appointmentid = request.getParameter("appointmentnormalnew");

			String description = request.getParameter("description");

			boolean exceedLimit = false;

			String effectiveDate = request.getParameter("effectivedate");
			
			String year = "20" + request.getParameter("year");
			if(year.equals("20null")){
				year = "";
			}
			
			String diploma = request.getParameter("diploma");
			
			String saclass = request.getParameter("nameclass");
			
			System.out.println("THIS SHOULD BE PRINTED " +staffs + appointmentid + description + effectiveDate + year + diploma + saclass);
			if (description != null && description != "") {
				// New Group to be created
				System.out.println("Creating new sub group");
				groupNumber = Database.StaffAppointmentGroupDB.getLastNumber() + 1;
				Database.StaffAppointmentGroupDB.createGroup(
						Integer.toString(groupNumber), description);
			} else {
				groupNumber = 1;
			}
			
			String tempStaff = "";
			//String tempAppointment = appointmentid;
			// loop for creating same appointment for multiple staff
			//for (String staffid : stafflist) {
				//for (String appointmentid1 : appointmentlist) {

					String md5 = Logic.MD5.convert(Long.toString(System
							.currentTimeMillis()) + staffs);

					if (Database.StaffAppointmentDB
							.numberOfAppointments(staffs) >= Database.MaxConcApptDB
							.select()) {
						exceedLimit = true;
					}

					System.out.println("Appointments:"
							+ Database.StaffAppointmentDB
									.numberOfAppointments(staffs));
					System.out.println("Max Conc:"
							+ Database.MaxConcApptDB.select());

					StaffAppointment sa = new StaffAppointment();
					sa.setAppointmentID(appointmentid);
					sa.setEffectiveDate(effectiveDate);
					sa.setApptGrpID(Integer.toString(groupNumber));
					sa.setLoginID(staffs);
					sa.setMd5(md5);
					sa.setYear(year);
					sa.setDiploma(diploma);
					sa.setCreatedBy(
							(String) request.getSession().getAttribute("username"));
					sa.setSaclass(saclass);
					if (Database.StaffAppointmentDB.checkDuplicateAppointmentEntry(sa)) {
						tempStaff = staffs;
						response.sendRedirect("newstaffapp_newappt.jsp?error=3&staffid=" + staffs);
						return;
					}
					Database.SpecificStaffAppointmentDB.createAppointmentSpecific(sa);
					if (exceedLimit) {
						response.sendRedirect("newstaffapp_newappt.jsp?prompt=true&id="
								+ md5);
						return;
					}
				//}
			//}

					AuditLogDB.insertLog("assign staff to appointment",
							(String) request.getSession().getAttribute("name"));
					response.sendRedirect("newstaffapp_newappt.jsp?error=-1");
		} else {
			response.sendRedirect("newstaffapp_newappt.jsp?error=1");
		}
		 	*/
	}
	

}

